<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增分卷')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-shunt-add" th:object="${processShunt}">
            <input name="sign" value="2" type="hidden">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">合并材料编号：</label>
                <div class="col-sm-8">
                    <select name="ribbonId" id="ribbonId" class="form-control select2-multiple" onchange="getSurface(this.value)">
                        <option value="">请选择</option>
                        <option th:each="dict : ${ribbons}" th:text="${dict.ribbonNumber}" th:value="${dict.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">原膜编号：</label>
                <div class="col-sm-8" id="num">
                    <!--<select name="inventoryId" class="form-control m-b" onchange="getMsg(this.value)">-->
                        <!--<option value="">请选择</option>-->
                        <!--<option th:each="dict : ${inventorys}"  th:text="${dict.number}" th:value="${dict.id}"></option>-->
                    <!--</select>-->
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">厂家名称：</label>
                <div class="col-sm-8">
                    <input name="factoryName" id="factoryName" th:field="*{factoryName}" class="form-control" disabled type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">材料名称：</label>
                <div class="col-sm-8">
                    <input name="materialsName" id="materialsName" th:field="*{materialsName}" class="form-control" disabled type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">颜色：</label>
                <div class="col-sm-8">
                    <input name="color" id="color" disabled th:field="*{color}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">来料日期：</label>
                <div class="col-sm-8">
                    <input name="mDate" disabled class="form-control" th:field="*{mDate}" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">镀铝面：</label>
                <div class="col-sm-8">
                    <input name="surface" id="surface" class="form-control" type="hidden">
                    <input name="surfaceName" id="surfaceName" disabled class="form-control" type="text">
                    <!--<select name="surface" class="form-control m-b"-->
                            <!--th:with="type=${@dict.getType('process_ribbon_state')}">-->
                        <!--<option value="">请选择</option>-->
                        <!--<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>-->
                    <!--</select>-->
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">镀铝重量：</label>
                <div class="col-sm-8">
                    <input name="aluminizedWeigt" th:field="*{aluminizedWeigt}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">镀铝单位：</label>
                <div class="col-sm-8">
                    <input name="aluminizedUnit" th:field="*{aluminizedUnit}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">质量文字反馈：</label>
                <div class="col-sm-8">
                    <input name="qualityState" th:field="*{qualityState}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量图片反馈：</label>
                <div class="col-sm-4">
                    <input id="qualityImg" name="file" multiple type="file" data-show-caption="true">
                </div>
                <div class="col-sm-2">
                    <a class="btn btn-success" onclick='sendFileImg("qualityImg")'><i class="fa fa-plus">图片上传</i></a>
                </div>
                <div class="col-sm-1">
                    <a class="btn btn-warning" onclick='removeImg("qualityImg")'><i class="fa fa-plus">清空图片</i></a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div class="file-preview qualityImg" style="height: auto">
                    </div>
                    <input name="qualityImg" th:field="*{qualityImg}" type="hidden">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量视频反馈：</label>
                <div class="col-sm-6">
                    <input id="qualityVideo" name="file" multiple type="file" data-show-caption="true">
                </div>
                <div class="col-sm-1">
                    <a class="btn btn-success" onclick='sendFile("qualityVideo")'><i class="fa fa-plus">视频上传</i></a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div class="file-preview qualityVideo" style="height: auto">
                    </div>
                    <input name="qualityVideo" th:field="*{qualityVideo}" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作起始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" id="startTime" th:value="${#dates.format(processShunt.startTime, 'yyyy-MM-dd HH:mm:ss')}"
                               class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               data-type="datetime" data-sign=1 placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" id="endTime" th:value="${#dates.format(processShunt.endTime, 'yyyy-MM-dd HH:mm:ss')}"
                               class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               data-type="datetime" data-sign=2 placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">耗时：</label>
                <div class="col-sm-8">
                    <input id="consumeTimeBck" th:field="*{consumeTime}" disabled class="form-control" type="text">
                    <input name="consumeTime" id="consumeTime" th:field="*{consumeTime}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">
                <input type="hidden" name="operatingEquipment" th:field="*{operatingEquipment}" id="equaipment"/>
                <label class="col-sm-3 control-label">操作设备：</label>
                <div class="col-sm-8">
                    <select class="form-control" id="operat-select" onchange="chengeStatus(this.value)">
                        <option value="">请选择</option>
                        <option value="一号复卷机">一号复卷机</option>
                        <option value="二号复卷机">二号复卷机</option>
                        <option value="三号复卷机">三号复卷机</option>
                        <option value="四号复卷机">四号复卷机</option>
                        <option value="其他">其他</option>
                    </select>
                    <input id="other" placeholder="输入设备" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">操作班次：</label>
                <div class="col-sm-8">
                    <input name="operatingNumber" th:field="*{operatingNumber}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">操作人：</label>
                <div class="col-sm-8">
                    <select name="operator" id="operator" class="form-control" data-none-selected-text="请选择">
                        <option value="">请选择</option>
                        <option th:each="user : ${users}" th:text="${user.userName}" th:value="${user.userName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">分卷日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="ribbonTime" id="ribbonTime" th:value="${#dates.format(processShunt.ribbonTime, 'yyyy-MM-dd HH:mm:ss')}"
                               class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss"
                               placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "process/shunt";
        var imgs = [];
        $("#form-shunt-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            //操作人
            var select = $("#operat-select").val();
            if(select == "其他"){
                $("#equaipment").val($("#other").val());
            }else{
                $("#equaipment").val(select);
            }
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-shunt-add').serialize());
            }
        }
        function getSuffix(url){
            var indexOf = url.indexOf(".")+1;
            return url.slice(indexOf,url.length);
        }
        $(function () {
            $("#ribbonId").select2();
            var shunt = [[${processShunt}]];
            //合并材料
            $("#ribbonId option[value= " + shunt.ribbonId + "]").prop("selected",true);
            //原膜
            getSurface(shunt.ribbonId);
            $("#inventoryId option[value= " + shunt.inventoryId + "]").prop("selected",true);
            $("#operator option[value= " + shunt.operator + "]").prop("selected",true);
            if(shunt.qualityVideo!=null && shunt.qualityVideo!=""){
                if(getSuffix(shunt.qualityVideo)=="mp4"){
                    $(".qualityVideo").html('<video src="'+shunt.qualityVideo+'" controls="controls" style="height: 170px;width: 100%">');
                }else{
                    $(".qualityVideo").html('<img src="'+shunt.qualityVideo+'" style="height: 100%;width: 100%">');
                }
            }
            if(shunt.qualityImg!=null && shunt.qualityImg!=""){
                var p = "";
                var split = shunt.qualityImg.split(",");
                for (var i = 0; i < split.length; i++) {
                    imgs.push(split[i]);
                    p = p+'<img src="'+split[i]+'" style="height: 100%;width: 100%">';
                }
                $(".qualityImg").html(p);
            }
            var equipment = shunt.operatingEquipment;
            if(equipment!=null && equipment!=''){
                if(equipment == "一号复卷机" || equipment == "二号复卷机"
                    || equipment == "三号复卷机" || equipment == "四号复卷机"){
                    $("#operat-select option[value= " + equipment + "]").prop("selected",true);
                }else{
                    $("#operat-select option[value= '其他']").prop("selected",true);
                    $("#other").attr("type","text");
                    $("#other").val(equipment);
                }
            }
        });
        //多个图片上传
        function sendFileImg(name) {
            var formData = new FormData();
            //注入 name=file
            var files = $('#'+name)[0].files;
            if(files.length>10){
                layer.alert("上传上限位10");
                return false;
            }
            for (var i = 0; i < files.length; i++) {
                //注意：这里append进去的是File对象，而不是FileList对象
                formData.append("file", files[i]);
            }

            //  var formData = new FormData($('#'+name)[0]);
            // pp.append("file", val);
            $.ajax({
                type: "POST",
                url: ctx + "common/uploadNew",
                data: formData,
                processData: false,
                contentType: false,
                success: function(result) {
                    if (result!=null && result.code == web_status.SUCCESS) {
                        var a = result.url;
                        var p ="";
                        for (var i = 0; i < a.length; i++) {
                            imgs.push(a[i]);
                        }
                        for (var j = 0; j < imgs.length; j++) {
                            p=p+'<img src="'+imgs[j]+'" style="height: 100%;width: 100%">';
                        }
                        $('.'+name).html(p);
                        $('[name="'+name+'"]').val(imgs.join(","));
                    } else {
                        $.modal.alertError("上传失败");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败");
                }
            });
        }
        function removeImg(name) {
            imgs = [];
            $('.'+name).html("");
            $('#'+name).val("");
            $('[name="'+name+'"]').val("");
        }
        // 上传文件
        function sendFile(name) {
            var val = $('#'+name)[0].files;
            var indexOf = val[0].name.indexOf(".")+1;
            var s = val[0].name.slice(indexOf,val[0].name.length);
            var data = new FormData();
            data.append("file", val[0]);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result!=null && result.code == web_status.SUCCESS) {
                        if(s=="mp4"){
                            $('.'+name).html('<video src="'+result.url+'" controls="controls" style="height: 170px;width: 100%">');
                            $('[name="'+name+'"]').val(result.url);
                        }else{
                            $('.'+name).html('<img src="'+result.url+'" style="height: 100%;width: 100%">');
                            $('[name="'+name+'"]').val(result.url);
                        }
                    } else {
                        $.modal.alertError("上传失败");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败");
                }
            });
        }
        function chengeStatus(val) {
            if(val == "其他"){
                $("#other").attr("type","text");
            }else{
                $("#other").attr("type","hidden");
            }
        }
        function getMsg(value,id) {
            if(value!=null && value!=""){
                var data = [[${ribbons}]];
                var obj = {};
                for (var i = 0; i < data.length; i++) {
                    if(data[i].id == id){
                        var list = data[i].inventories;
                        for (var j = 0; j < list.length; j++) {
                            if(list[j].id == value){
                                obj = list[j];
                                break;
                            }
                        }
                    }
                }
                //赋值
                $("#materialsName").val(obj.materialsName);
                $("#factoryName").val(obj.factoryName);
                $("#color").val(obj.color);
                $("#mDate").val(obj.mDate);
            }
        }

        function getSurface(val) {
            console.log("出发事件");
            var arrays = [[${ribbons}]];
            var ints = {};
            for (var i = 0; i < arrays.length; i++) {
                if(val == arrays[i].id){
                    var sub = "-";
                    if(arrays[i].surface == 1){
                        sub = "内";
                    }else if(arrays[i].surface == 1){
                        sub = "外";
                    }else{
                        sub = "内+外";
                    }
                    $("#surface").val(arrays[i].surface);
                    $("#surfaceName").val(sub);
                    ints.list = arrays[i].inventories;
                    ints.id = arrays[i].id;
                }
            }
            var html = $("#goodsTypeTpl").tmpl(ints).html();
            $("#num").html(html);
        }
    </script>
</body>
</html>

<script id="goodsTypeTpl" type="text/x-jquery-tmpl">
<div>
    <select class='form-control m-b' name="inventoryId" id="inventoryId" onchange="getMsg(this.value,${id})">
        <option value="">所有</option>
        {{each(i,obj) list}}
            <option value="${obj.id}">${obj.number}</option>
        {{/each}}
    </select>
</div>
</script>